//
//  ReadingViewController.m
//  FreeReading
//
//  Created by lanou3g on 15/10/27.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "ReadingViewController.h"


@interface ReadingViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;


@end

@implementation ReadingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
     [self addScroll];
    [self addTableView];
    // 取消内边距
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBar"] forBarMetrics:(UIBarMetricsDefault)];
    
    NSArray *arr = [[NSArray alloc] initWithObjects:@"涨姿势", @"健身房", nil];
    // 用数组初始化 分段按钮
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:arr];
    segment.frame = CGRectMake((kScreenWidth - 200) / 2, 0, 200, 30);
    segment.layer.cornerRadius = 15;
    
    segment.tintColor = [UIColor redColor];
    
    segment.layer.borderColor = [UIColor redColor].CGColor;
    segment.tintColor = [UIColor whiteColor];
    segment.layer.borderColor = [UIColor lightGrayColor].CGColor;
    segment.layer.borderWidth = 1;
    segment.layer.masksToBounds = YES;
    
    // 设置选中的索引
    segment.selectedSegmentIndex = 0;
    
    [segment addTarget:self action:@selector(actionSegment:) forControlEvents:(UIControlEventValueChanged)];
    segment.tag = 1000;
    
    [self.navigationController.navigationBar addSubview:segment];
    self.navigationItem.titleView = segment;
    
}

// 实现点击方法
- (void)actionSegment:(UISegmentedControl *)segment
{

    switch (segment.selectedSegmentIndex) {
        case 0:
            
            self.scrollView.contentOffset = CGPointMake(0, 0);
            
            
            
            break;
            
            case 1:
            
            self.scrollView.contentOffset = CGPointMake(kScreenWidth, 0);
            
            
            break;
        default:
            break;
    }
    
    
}


// 添加tableView
- (void)addTableView
{
   
    self.postureView = [[PostureViewController alloc] init];
    self.healthView = [[HealthsViewController alloc] init];
    [self addChildViewController:self.healthView];
    [self addChildViewController:self.postureView];
    self.postureView.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 120);
    self.healthView.tableView.frame = CGRectMake(kScreenWidth, 0, kScreenWidth , kScreenHeight - 64);
    [self.scrollView addSubview:self.postureView.tableView];
    [self.scrollView addSubview:self.healthView.tableView];
    
}


- (void)addScroll
{
    // 创建一个UIScrollView
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    
    // 设置属性
    _scrollView.backgroundColor = [UIColor whiteColor];
    // scrollView的显示范围 能滑动的范围
    _scrollView.contentSize = CGSizeMake(kScreenWidth * 2, kScreenHeight);
    // 整页滚动
    _scrollView.pagingEnabled = YES;
    // 边界回弹
    _scrollView.bounces = NO;
    // 水平滚动条是否显示
    _scrollView.showsHorizontalScrollIndicator = YES;
    _scrollView.showsVerticalScrollIndicator = YES;
    
    // 最重要的 就是一堆代理方法
    _scrollView.delegate = self;
    
    
    [self.view addSubview:self.scrollView];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.hidesBottomBarWhenPushed = YES;
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.hidesBottomBarWhenPushed = NO;
}

#pragma mark - UIScrollDelegata
// 开始
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    // 改变segment 的 位置
    UISegmentedControl *segement = (UISegmentedControl *)[self.navigationController.navigationBar viewWithTag:1000];
    // scrollView 的偏移量的x坐标除以宽度
    
    segement.selectedSegmentIndex = scrollView.contentOffset.x / kScreenWidth;
    
}

#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if ([self isViewLoaded] && self.view.window == nil) {
        
        self.view = nil;
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
